/**
 * Filename: LocalManageAdapter.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/17 15:22
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.LocalMusicManageActivity;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageUtils;
import com.github.postsanf.yinian.utils.LocalViewHolder;

import java.util.List;

public class LocalMusicManageAdapter extends CommonAdapter<String>
{
	private  Activity   activity;
	private  Resources mResource;
	// 文件夹路径
	private String mDirPath;
	public LocalMusicManageAdapter(Context context, List<String> mDatas, int itemLayoutId,
								   String dirPath, Activity activity)
	{
		super(context, mDatas, itemLayoutId,1);
		this.mDirPath = dirPath;
		this.activity = activity;
	}

	public void setResource(Resources mResource){
		this.mResource = mResource;
	}

	@Override
	public void convert(final LocalViewHolder helper, final String item ,final Boolean isFirst)
	{
		final SimpleDraweeView mImageView = helper.getView(R.id.iv_album_grid_item_image);
		final ImageView mSelect = helper.getView(R.id.id_item_select);

		if(isFirst)
		{
			helper.setResource(mResource);
			helper.setImageResource(R.id.iv_album_grid_item_image,R.drawable.tocamera);
			mSelect.setVisibility(View.GONE);
		}
		else
		{
			//设置no_pic
//			helper.setImageResource(R.id.iv_album_grid_item_image, R.drawable.pictures_null);
			//设置no_selected
			helper.setImageResource(R.id.id_item_select,R.drawable.picture_unselected);

			//设置图片
			helper.setImageByUrl(R.id.iv_album_grid_item_image, mDirPath + "/" + item);
			mSelect.setVisibility(View.VISIBLE);
		}
		// 设置图片宽高
		int screenWidth = DisplayUtils.getScreenWidthPixels(activity);
		int horizontalSpacing = DisplayUtils.dp2px(activity, 2);
		int itemWidth = (screenWidth - 2 * horizontalSpacing) / 3;
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(itemWidth, itemWidth);
		mImageView.setLayoutParams(params);
		mImageView.setColorFilter(null);
		//设置ImageView的点击事件
		mImageView.setOnClickListener(new View.OnClickListener() {
			//选择，则将图片变暗，反之则反之
			@Override
			public void onClick(View v) {
				if (isFirst) {
					ImageUtils.pickImageFromCamera(activity);
					return;
				}
				String path = mDirPath + "/" + item;
				// 已经选择过该图片
				if (SelectedImg.mSelectedImage.contains(path)) {
					SelectedImg.mSelectedImage.remove(path);
					if (mResource != null) {
						mSelect.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("picture_unselected", CommonConstants.THEME_DRAWABLE,CommonConstants.THEME_PACKAGE)));
					} else {
						mSelect.setImageResource(R.drawable.picture_unselected);
					}
					mImageView.setColorFilter(null);
				} else
				// 未选择该图片
				{
					SelectedImg.mSelectedImage.add(path);
					if (mResource != null) {
						mSelect.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("pictures_selected", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
					} else {
						mSelect.setImageResource(R.drawable.pictures_selected);
					}
					mImageView.setColorFilter(Color.parseColor("#33000000"));
				}
				((LocalMusicManageActivity) activity).setSureCount(SelectedImg.mSelectedImage.size());
			}
		});

		// 已经选择过的图片，显示出选择过的效果
		if (SelectedImg.mSelectedImage.contains(mDirPath + "/" + item))
		{
			if (mResource != null) {
				mSelect.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("pictures_selected", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
			} else {
				mSelect.setImageResource(R.drawable.pictures_selected);
			}
			mImageView.setColorFilter(Color.parseColor("#33000000"));
		}
	}
}